package org.opensplice.restful.service;

import java.lang.reflect.Method;

import Chat.ChatMessageTypeSupport;
import DDS.ANY_STATUS;
import DDS.DATAREADER_QOS_USE_TOPIC_QOS;
import DDS.DataReader;
import DDS.DomainParticipant;
import DDS.DomainParticipantFactory;
import DDS.DomainParticipantQosHolder;
import DDS.ReliabilityQosPolicyKind;
import DDS.Subscriber;
import DDS.SubscriberQosHolder;
import DDS.TOPIC_QOS_DEFAULT;
import DDS.Topic;
import DDS.TopicQosHolder;

/*
 * Simple tester just to register a ChatMessage type with DDS and inject a Topic for it.
 * After that, a Subscriber and DataReader are created for it, making it
 * possible to query the data via HTTP.
 * After <sleepSecs> seconds, the class destructs itself.
 * This class tests the fundamentals of the interactions with OpenSplice.
 */

public class DDSTester {
    
    private static final String TYPE_SUPPORT_CLASS_SUFFIX = "TypeSupport";
    private static final String REGISTER_TYPE_METHOD = "register_type";

    public static void test(long sleepSecs) {
        String partitionName = "MyPartition";
        String topicName = "Chat_ChatMessage";
        String typeName = "Chat::ChatMessage";
        TopicQosHolder reliableTopicQos = new TopicQosHolder();
        Topic topic;
        int status;

        DomainParticipantFactory dpf = DDS.DomainParticipantFactory.get_instance ();
        ErrorHandler.checkHandle(dpf, "DDS.DomainParticipantFactory.get_instance");
        DomainParticipantQosHolder dpQos = new DomainParticipantQosHolder();
          status = dpf.get_default_participant_qos (dpQos);
          ErrorHandler.checkStatus(status, "DDS.DominParticipantFactory.get_default_participant_qos");
        DomainParticipant participant = dpf.create_participant(null, dpQos.value, null, DDS.ANY_STATUS.value);
        ErrorHandler.checkHandle(participant, "DDS.DomainParticipantFactory.create_participant");
        dpQos = null;
        
        SubscriberQosHolder subQos = new SubscriberQosHolder();
        status = participant.get_default_subscriber_qos (subQos);
        ErrorHandler.checkStatus(status, "DDS.DomainParticipant.get_default_subscriber_qos");
        subQos.value.partition.name = new String[1];
        subQos.value.partition.name[0] = partitionName;
        Subscriber subscriber = participant.create_subscriber(subQos.value, null, ANY_STATUS.value);    
        ErrorHandler.checkHandle(subscriber, "DDS.DomainParticipant.create_subscriber");
        subQos = null;
        
        ChatMessageTypeSupport typeSupport = new ChatMessageTypeSupport();
        ErrorHandler.checkHandle(
            typeSupport, "new ChatMessageTypeSupport");
        typeName = typeSupport.get_type_name();
        status = typeSupport.register_type(participant, typeName);
        ErrorHandler.checkStatus(
            status, "track.ChatMessageTypeSupport.register_type");

        /* Set the ReliabilityQosPolicy to RELIABLE to make it work with the
         *   OpenSplice example */
        status = participant.get_default_topic_qos(reliableTopicQos);
        ErrorHandler.checkStatus(
            status, "DDS.DomainParticipant.get_default_topic_qos");
        reliableTopicQos.value.reliability.kind =
            ReliabilityQosPolicyKind.RELIABLE_RELIABILITY_QOS;

        topic = participant.create_topic(topicName, typeName, reliableTopicQos.value,
                null, ANY_STATUS.value);
        ErrorHandler.checkHandle(topic, "DDS.DomainParticipant.create_topic");
        
        DataReader reader = subscriber.create_datareader( 
                   topic, 
                   DATAREADER_QOS_USE_TOPIC_QOS.value,
                   null,
                   ANY_STATUS.value);
        ErrorHandler.checkHandle(reader, "DDS.Subscriber.create_datareader");
        
        try {
            Thread.sleep(1000*sleepSecs);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        status = participant.delete_contained_entities();
        ErrorHandler.checkStatus(status, "DDS.DomainParticipant.delete_contained_entities");
        status = dpf.delete_participant(participant);
        ErrorHandler.checkStatus(status, "DDS.DomainParticipantFactory.delete_participant");
    }
}
